package com.lingdian.fund.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import jodd.datetime.JDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.lingdian.common.vo.JSONResult;
import com.lingdian.fund.bean.Fund;
import com.lingdian.fund.bean.FundData;
import com.lingdian.fund.bean.FundUser;
import com.lingdian.fund.repository.FundDataReporsitory;
import com.lingdian.fund.repository.FundRepository;
import com.lingdian.fund.service.FundDataService;
import com.lingdian.fund.service.SpiderDataService;
import com.lingdian.fund.service.SubjectService;
import com.lingdian.fund.service.impl.FundDataSubject;
import com.lingdian.fund.vo.FundVO;
import com.lingdian.member.bean.Member;

@Controller
@RequestMapping("/funddata")
public class FundDataController {

	@Autowired
	private SpiderDataService spiderDataService;
	@Autowired
	private SubjectService fundDataSubject;
	@Autowired
	private FundRepository fundRepository;
	@Autowired
	private FundDataReporsitory fundDataReporsitory;
	@Autowired
	private FundDataService fundDataService;
	@RequestMapping("/updateAll")
	public void updateAll(){
		spiderDataService.threadUpdateAll();
	}
	
	@RequestMapping("/findFundAll")
	public @ResponseBody JSONResult findFundAll(
			@RequestParam(value="page", defaultValue="1") int page,
			@RequestParam(value="size", defaultValue="10")int size){
//		Page<Fund> fundPage = fundRepository.findAll(new PageRequest(page, size));
		String beginDate = new JDateTime().addYear(-2).toString("YYYY-MM-DD");
		List<Fund> fundList = fundDataService.querySmart(beginDate, new JDateTime().addDay(-1).toString("YYYY-MM-DD"));
		fundList = fundList.subList(0, page*size);
		return new JSONResult(0, fundList);
	}
	@RequestMapping("/findFundDataByCode")
	public @ResponseBody JSONResult findFundDataByCode(String code){
		List<FundData> fundDataList = fundDataReporsitory.findByCodeOrderByFundDate(code);
		return new JSONResult(0, fundDataList);
	}
	@RequestMapping("/findByMaxUnitValue")
	@ResponseBody
	public  List<Object[]> findByMaxUnitValue(){
		System.out.println("xxx");
		return spiderDataService.findByMaxUnitValue("");
	}
	@RequestMapping("/updateOne")
	public @ResponseBody JSONResult updateOne(String code,String startDate,String endDate){
		if(StringUtils.isEmpty(endDate)){
			endDate = new JDateTime().toString("YYYY-MM-DD");
		}
		spiderDataService.updateOne(code, startDate, endDate);
		return new JSONResult(0, "");
	}
	@RequestMapping("/queryFundDataByCode")
	public @ResponseBody JSONResult queryFundDataByCode(String code,String startDate,String endDate){
		if(StringUtils.isEmpty(endDate)){
			endDate = new JDateTime().toString("YYYY-MM-DD");
		}
//		spiderDataService.updateOne(code, startDate, endDate);
		List<FundData> list = fundDataReporsitory.findByCodeAndFundDateBetweenOrderByFundDate(code, startDate, endDate);
		return new JSONResult(0, list);
	}
	@RequestMapping("/test")
	public @ResponseBody JSONResult testObserver(){
		FundUser user1 = new FundUser();
		user1.setUserName("t1");
		FundUser user2 = new FundUser();
		user2.setUserName("t2");
		fundDataSubject.addObserver(user1);
		fundDataSubject.addObserver(user2);
		FundData fundData = new FundData();
		fundData.setCode("xxxxx");
		((FundDataSubject)fundDataSubject).setFundData(fundData);
		return new JSONResult(0, "");
	}
	@RequestMapping("/listPage")
	public String listPage(){
		return "fund/list";
	}
	@RequestMapping("/listRangeFundPage")
	public ModelAndView listRangeFundPage(HttpSession session){
//		Member member = (Member) session.getAttribute("member");
		ModelAndView mv = new ModelAndView("fund/listRangeFundData");
//		mv.addObject("sessionMember",member);
		return mv;
	}
	@RequestMapping("/listRangeFund")
	public @ResponseBody JSONResult listRangeFund(String beginDate){
		if(StringUtils.isEmpty(beginDate)){
			beginDate = new JDateTime().addYear(-1).toString("YYYY-MM-DD");
		}
		String nowDate = new JDateTime().addMonth(-3).toString("YYYY-MM-DD");
//		List<Object[]> vos = fundDataReporsitory.findData(beginDate,nowDate);
		List<FundVO> vos = fundDataService.querySmartOrderByRangeData(beginDate, nowDate);
		return new JSONResult(0, vos);
	}
}
